<template>
  <div class="container">
    <div class="title">新建用户</div>
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form :model="form" :rules="rules" status-icon ref="form" label-width="100px" @submit.native.prevent>
            <el-form-item label="用户名" prop="username">
              <el-input size="medium" v-model="form.username" placeholder="请填写用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="form.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirm_password">
              <el-input type="password" v-model="form.confirm_password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input type="medium" v-model="form.email" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="仓库分组">
              <el-checkbox-group v-model="form.codes" @change="handleCheckedCitiesChange">
                <el-checkbox v-for="item in HaveList" :label="item.code" :key="item.code">{{
                  item.name
                }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item class="submit">
              <el-button type="primary" @click="submitForm('form')" :loading="loading">保 存</el-button>
              <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import wmsuser from '@/model/omsuser'

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.form.confirm_password !== '') {
          this.$refs.form.validateField('confirm_password')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      form: {
        username: '',
        email: '',
        password: '',
        confirm_password: '',
        codes: []
      },
      rules: {
        password: [{ required: true, validator: validatePass, trigger: 'blur' }],
        confirm_password: [{ required: true, validator: validatePass2, trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      },
      HaveList: []
    }
  },
  created() {
    this.getHaveList()
  },
  methods: {
    async submitForm(formName) {
      try {
        this.loading = true
        console.log(this.form)
        const res = await wmsuser.createWmsUser(this.form)
        this.loading = false
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.$message.success(`${res.message}`)
          this.resetForm(formName)
        }
      } catch (error) {
        this.loading = false
        this.$message.error(error)
        console.log(error)
      }
    },
    async getHaveList() {
      const res = await wmsuser.gitUserHave()
      console.log(res)
      this.HaveList = res
    },
    handleCheckedCitiesChange() {

    },

    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
